package com.loyin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;

import com.et.mvc.JsonView;
import com.et.mvc.View;
import com.loyin.model.Manager;
import com.loyin.model.ManagerVo;
import com.loyin.util.MD5;
import com.loyin.util.Page;
/***
 * 管理员
 * @author 刘声凤
 * 2011-12-20
 */
public class ManagerController  extends ApplicationController {
	private Logger log=Logger.getLogger(this.getClass());
	public void list(){
		Manager Manager = new Manager();
		StringBuffer sql = new StringBuffer("select * from igs_Manager where 1=1 ");
		try {
			String pagenostr=request.getParameter("pageNo");
			String pagesizestr=request.getParameter("pageSize");
			if(pagenostr==null||"".equals(pagenostr.trim())){
				pagenostr="1";
			}
			if(pagesizestr==null||"".equals(pagesizestr.trim())){
				pagesizestr=this.pagesize+"";
			}
			Page page=new Page(0,Integer.parseInt(pagenostr),Integer.parseInt(pagesizestr),null);
			List<Object> paramlist=new ArrayList<Object>();
			
			List<Manager> list = (List<Manager>) Manager.findBySql(Manager.getClass(), sql.toString(), paramlist.toArray(),"",page.getPageSize(),page.start);
			long count=Manager.count(Manager.getClass(),null, paramlist.toArray());
			page=new Page(count,Integer.parseInt(pagenostr),Integer.parseInt(pagesizestr),list);
			request.setAttribute("page",page);
		} catch (Exception e) {
			log.error("",e);
			request.setAttribute("msg_error", "访问出错");
		}
	}
	public void reg(){}
	public void home(){}
	
	public View save(Manager manager){
		Map<String,Object> map=new HashMap<String,Object>();
		String checkcode=request.getParameter("checkcode");
		String ckcode=(String)session.getAttribute("check");
		if(checkcode==null||!checkcode.equals(ckcode)){
			map.put("stat", "F");
			map.put("msg", "验证码错误！");
			return new JsonView(map);
		}
		try{
			if(manager.id==null||manager.id==0){
				List<Manager> ulist=(List<Manager>)manager.findBySql(manager.getClass(), "select * from igs_manager where usercode=? ", new Object[]{manager.usercode});
				if(ulist!=null&&ulist.isEmpty()==false){
					map.put("stat", "F");
					map.put("msg", "已经存在此管理员！");
					return new JsonView(map);
				}
				manager.setPwd(MD5.getMD5ofStr(manager.getPwd()));
			}
			if(manager.save()==true){
				map.put("stat", "S");
				map.put("msg", "注册管理员信息成功！");
				map.put("arg",manager.id);
			}else{
				map.put("stat", "F");
				map.put("msg", "注册管理员信息失败！");
			}
		}catch(Exception e){
			log.error("注册异常",e);
			map.put("stat", "E");
			map.put("msg", "注册管理员异常！");
		}
		return new JsonView(map);
	}
	public View login(){
		Map<String,Object> map=new HashMap<String,Object>();
		String usercode=request.getParameter("usercode");
		String pwd=request.getParameter("pwd");
		String checkcode=request.getParameter("checkcode");
		String ckcode=(String)session.getAttribute("check");
		Manager manager=new Manager();
		try{
			if(checkcode!=null&&checkcode.equals(ckcode)){
			 	
				List<Manager> managerlist=(List<Manager>)manager.findBySql(manager.getClass(), "select ID,usercode,role,pwd from igs_manager where usercode=? and pwd=? ",
			 												new Object[]{usercode,usercode,usercode,MD5.getMD5ofStr(pwd)});
			 	if(managerlist==null||managerlist.isEmpty()==true){
			 		map.put("stat", "F");
					map.put("msg", "用户名或密码错误！");
			 	}else{
			 		Manager managerinfo=managerlist.get(0);
			 		ManagerVo managervo=new ManagerVo();
			 		BeanUtils.copyProperties(managervo,managerinfo);
			 		session.setAttribute("manager_session", managervo);
			 		map.put("stat", "S");
					map.put("msg", "");
			 	}
			}else{
				map.put("stat", "F");
				map.put("msg", "验证码错误！");
			}
		}catch(Exception e){
			log.error("",e);
			map.put("stat", "F");
			map.put("msg", "登录异常！");
		}
		return new JsonView(map);
	}
	public View logout(){
		Map<String,Object> map=new HashMap<String,Object>();
		session.removeAttribute("manager_session");
		map.put("stat", "S");
		return new JsonView(map);
	}
	public void loginform(){}
}
